package com.pay.controller;

import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.pay.beans.ReturnBean;
import com.pay.commons.utils.lang.StringUtils;
import com.pay.commons.utils.web.HttpClientUtils;
import com.pay.commons.utils.web.HttpClientUtils.Method;
import com.pay.constants.RedisKeyConstants;
import com.pay.constants.RetConstants;
import com.pay.util.NeedOpenId;
import com.pay.util.RedisUtil;

@Controller
public class FeedbackInfoController {
	static Log log = LogFactory.getLog(FeedbackInfoController.class);
	
	private static Properties prop = new Properties();

	static {
		try {
			prop.load(FeedbackInfoController.class.getClassLoader().getResourceAsStream("system.properties"));
		} catch (Exception e) {
			log.error("", e);
		}
	}
	
	/**
	 * Description  反馈意见
	 */
	@RequestMapping(value="feedback",produces = "text/html;charset=UTF-8")
	@ResponseBody
	@NeedOpenId(needOpenId = true, isLoginFlag = false)
	public String feedback(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value = "customerNo", required = false) String customerNo,@RequestParam String source){
		response.setHeader("Access-Control-Allow-Origin", "*");
		String openId = request.getParameter("openId"); 
		customerNo = RedisUtil.getValue(RedisKeyConstants.MULT_ACCOUNT_DEFAULT_OPENID_USERNO + openId);
		String content = request.getParameter("content"); //意见反馈内容
		log.info("login logincheck log :" + customerNo + ",source:" + source + ",content:" + content + ",openId:" + openId  );
		if(StringUtils.isBlank(customerNo)){
			return JSON.toJSONString(new ReturnBean(RetConstants.SYSTEM_ERROR, "商户编号不能为空"));
		}
		if(StringUtils.isBlank(content)){
			return JSON.toJSONString(new ReturnBean(RetConstants.SYSTEM_ERROR, "意见反馈不能为空"));
		}
		try {
			String mesServiceUrl = (String) prop.get("ky_mes_url");
			String ky_login = (String) prop.get("ky_login");
			String data = "/user/feedback?content="
					+ URLEncoder.encode(content, "UTF-8")+ "&source="				
					+ source + "&customerNo="
					+ customerNo + "&test="
					+ ky_login;
			long startTime = System.currentTimeMillis();
			log.info(String.format("feedback.mesServiceUrl  begin. reqUrl=%s conn time=%s",mesServiceUrl + data, startTime));
			String resData = HttpClientUtils.send(Method.POST,mesServiceUrl + data, "", true, Charset.forName("UTF-8").name(), 18000);
			long endTime = System.currentTimeMillis();
			log.info(String.format("feedback.mesServiceUrl  end. conn time=%s,result=%s",
							(endTime - startTime), resData));
			JSONObject jsStr = JSONObject.fromObject(resData);
			if(!"0000".equals(jsStr.get("returnCode"))){
				return JSON.toJSONString(new ReturnBean(RetConstants.SYSTEM_ERROR, "提交失败"));	
			}
			return JSON.toJSONString(new ReturnBean(RetConstants.SUCCESS, "提交成功"));
		} catch (Exception e) {
			log.error("意见反馈提交失败     feedback error customerNo:" + customerNo);
			log.info("", e);
			return JSON.toJSONString(new ReturnBean(RetConstants.ERROR, "提交失败"));
		}
	}




  
}
